package com.tledu.equipment.aspect;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.equipment.domain.EquipmentInfo;
import com.tledu.equipment.domain.EquipmentLog;
import com.tledu.equipment.domain.EquipmentUse;
import com.tledu.equipment.mapper.EquipmentLogMapper;
import com.tledu.equipment.mapper.EquipmentUseMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 369
 * @CreateTime: 2024-09-03  10-09
 */
@Aspect
@Component
@Slf4j
public class LogAutoFillAspect {
    @Autowired
    private EquipmentLogMapper equipmentLogMapper;
    @Autowired
    private EquipmentUseMapper equipmentUseMapper;

    @Pointcut("execution(* com.tledu.equipment.mapper.EquipmentInfoMapper.*(..)) " +
            "&& @annotation(com.tledu.equipment.aspect.MyEquipmentLog)") // 切点表达式，根据你的实际包路径设置
    public void serviceLayer() {
    }

    @Around("serviceLayer()")
    public Object logBefore(ProceedingJoinPoint pjp) throws Throwable {
        log.info("参试设备操作日志开始添加....");
        Object result = pjp.proceed();
        //获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) pjp.getSignature();//方法签名对象
        MyEquipmentLog autoFill = signature.getMethod().getAnnotation(MyEquipmentLog.class);//获得方法上的注解对象
        OperationType operationType = autoFill.value();//获得数据库操作类型

        //获取到当前被拦截的方法的参数--实体对象
        Object[] args = pjp.getArgs();
        if (args == null || args.length == 0) {
            return null;
        }
        //准备赋值的数据
        Long currentId = SecurityUtils.getUserId();

        //根据当前不同的操作类型，为对应的属性通过反射来赋值
        if (operationType == OperationType.PUT_IN_STORE) {
            //入库时,传参是id
            EquipmentLog euipmentLog = new EquipmentLog();

            euipmentLog.setEquipmentId((Long) args[0]);
            euipmentLog.setCreateUser(currentId);
            euipmentLog.setCreateTime(DateUtils.getNowDate());
            euipmentLog.setOperationType(3L);
            euipmentLog.setDelFlag(0L);
            equipmentLogMapper.insertEquipmentLog(euipmentLog);



        } else if (operationType == OperationType.PUT_IN_FACTORY) {
            EquipmentLog euipmentLog = new EquipmentLog();
            EquipmentInfo entity = (EquipmentInfo) args[0];

            euipmentLog.setEquipmentId(entity.getId());
            euipmentLog.setOperationType(0L);
            euipmentLog.setCreateUser(currentId);
            euipmentLog.setCreateTime(DateUtils.getNowDate());
            euipmentLog.setDelFlag(0L);
            equipmentLogMapper.insertEquipmentLog(euipmentLog);

        } else if (operationType == OperationType.GET_OUT_STORE){
            //出库时,传参是id
            EquipmentLog euipmentLog = new EquipmentLog();

            euipmentLog.setEquipmentId((Long) args[0]);
            euipmentLog.setCreateUser(currentId);
            euipmentLog.setCreateTime(DateUtils.getNowDate());
            euipmentLog.setOperationType(2L);
            euipmentLog.setDelFlag(0L);
            equipmentLogMapper.insertEquipmentLog(euipmentLog);

            EquipmentUse equipmentUse =new EquipmentUse();
            equipmentUse.setEquipmentId((Long) args[0]);
            equipmentUse.setCreateUser(currentId);
            equipmentUse.setCreateTime(DateUtils.getNowDate());

            equipmentUseMapper.insertEquipmentUse(equipmentUse);
        }else {
            //出场,传参是ids,Long[]
            EquipmentLog euipmentLog = new EquipmentLog();
            Long[] ids = (Long[]) args[0];
            for (Long id : ids) {
                euipmentLog.setEquipmentId(id);
                euipmentLog.setCreateUser(currentId);
                euipmentLog.setCreateTime(DateUtils.getNowDate());
                euipmentLog.setOperationType(1L);
                euipmentLog.setDelFlag(0L);
                equipmentLogMapper.insertEquipmentLog(euipmentLog);
            }


        }
        return result;
    }
}
